package com.dofun.locationservice.ui.widget;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

import com.dofun.locationservice.R;


//import android.widget.AbsListView;

/**
 * 自定义控件：下拉刷新,上拉加载
 * Created by gzs on 2016/4/9.
 */
//public class RefreshLayout extends SwipeRefreshLayout implements AbsListView.OnScrollListener {
public class RefreshLayout extends SwipeRefreshLayout {

    //最小滑动距离
    private int mTouchSlop;

    //ListView的加载中footer
    private View mListViewFooter;

    //按下时的y坐标
    private int mYDown;

    //抬起时的y坐标, 与mYDown一起用于滑动到底部时判断是上拉还是下拉
    private int mLastY;

    //是否完成分页加载
    private boolean iscompleteLoading=false;

    //上拉监听器, 到了最底部的上拉加载操作
    private OnLoadListener mOnLoadListener;

    //是否在加载中(上拉加载更多)
    private boolean isLoading = false;

    /**
     * listview实例
     */
//    private ListView mListView;
    private RecyclerView recyclerView;

    //底部‘加载更多’布局的数量
    private int mFootViewCount = 0 ;

    public void setmFootViewCount(int mFootViewCount) {
        this.mFootViewCount = mFootViewCount;
    }


//    private OnScrollListener loadingMoreListener;

    public RefreshLayout(Context context) {
        super(context);
    }

    public RefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        mListViewFooter = LayoutInflater.from(context).inflate(R.layout.core_listview_footer, null, false);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        // 初始化ListView对象
//        if (mListView == null) {
        if (recyclerView == null) {
            getListView();
        }
    }

    /**
     * 获取ListView对象
     */
    private void getListView() {
        int childs = getChildCount();
        if (childs > 0) {
            View childView = getChildAt(0);
//            if (childView instanceof ListView) {
//                mListView = (ListView) childView;
//                // 设置滚动监听器给ListView, 使得滚动的情况下也可以自动加载
//                mListView.setOnScrollListener(this);
//            }
            if (childView instanceof RecyclerView) {
                recyclerView = (RecyclerView) childView;
                // 设置滚动监听器给ListView, 使得滚动的情况下也可以自动加载
//                recyclerView.setOnScrollListener(this);
                recyclerView.addOnScrollListener(loadingMoreListener);
            }
        }
    }

    /**
     * 与下面事件分发dispatchTouchEvent同样作用，用于上拉加载时的底部判断
     * 不同之处在于此方法在滑动过程中，到底部会自动调用
     * 下面方法需要再次上拉时调用加载
     */
    OnScrollListener loadingMoreListener =new OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);

            //方法二： 通过是否正在加载和canScrollVertically 来判断
//            if(!isLoading && !recyclerView.canScrollVertically(1)){
//                isLoading = true;
//                loadData();
//            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

//            if (dy > 0) //向下滚动
//            {
//                int visibleItemCount = mLinearLayoutManager.getChildCount();
//                int totalItemCount = mLinearLayoutManager.getItemCount();
//                int pastVisiblesItems = mLinearLayoutManager.findFirstVisibleItemPosition();
//
//                //方法一：  第一个可见item的位置 + 当前可见的item个数 >= item的总个数 则已经到了底部

//                if (!isLoading && (visibleItemCount + pastVisiblesItems) >= totalItemCount) {
//                    isLoading = true;
//                    loadMoreDate();
//                }
//            }
        }
    };

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action){
            case MotionEvent.ACTION_DOWN: //按下
                mYDown = (int)ev.getRawY();
                break;
            case MotionEvent.ACTION_MOVE: //移动
                mLastY = (int)ev.getRawY();
                break;
            case MotionEvent.ACTION_UP: //抬起
                if(canLoad()){
                    loadData();
                }
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 是否可加载更多
     * @return
     */
    private boolean canLoad() {
//        return isPullUp() && !isLoading && isBottom();
        //TODO 该方法canScrollVertically()使用时会报错
        return isPullUp() && !isLoading && !recyclerView.canScrollVertically(1);
    }

    /**
     * 判断是否到了ListView底部
     * @return
     */
    private boolean isBottom(){
        if(recyclerView!=null && recyclerView.getAdapter()!=null){
//            return recyclerView.getLastVisiblePosition() == (recyclerView.getAdapter().getItemCount()-1);
        }
        return false;
    }

    /**
     * 是否是上拉操作
     * @return
     */
    private boolean isPullUp(){
        return (mYDown - mLastY) >= mTouchSlop;
    }

    /**
     * 加载数据
     */
    private void loadData(){
        if(iscompleteLoading){   //完成加载，不需要再进行分页加载数据
            //TODO
            return;
        }


        if(mOnLoadListener!=null){
            setLoading(true);
            mOnLoadListener.onPageLoad();
        }
    }

//    private void setFootLoading(){
//        View view = LayoutInflater.from()
//    }

    /**
     * 是否显示分页加载,true显示,
     * @param loading
     */
    public void setLoading(boolean loading) {
        this.isLoading = loading;
        if(isLoading){
//            recyclerView.addFooterView(mListViewFooter);
        }else{
//            recyclerView.removeFooterView(mListViewFooter);
            mYDown = 0;
            mLastY = 0;
        }
    }

    /**
     * 设置完成加载
     * @param iscompleteLoading
     */
    public void setIscompleteLoading(boolean iscompleteLoading) {
        this.iscompleteLoading = iscompleteLoading;
    }

    /**
     * 完成分页加载
     */
    public void completePageData() {
        setLoading(false);
        iscompleteLoading=true;
    }

    public int getColorPrimary(){
//        return AttrUtils.getValueOfColorAttr(getContext(),
//                R.styleable.MyTheme, R.styleable.MyTheme_colorPrimary);
//                R.styleable.Theme, R.styleable.Theme_colorPrimary);
        return R.color.colorAccent;
    }

//    @Override
//    public void onScrollStateChanged(AbsListView view, int scrollState) {
//
//    }
//
//    @Override
//    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//        // 滚动时到了最底部也可以加载更多
//        if (canLoad()) {
//            loadData();
//        }
//    }

    public void setOnLoadListener(OnLoadListener loadListener) {
        setColorSchemeColors(getColorPrimary());
        mOnLoadListener = loadListener;
    }

    /**
     * 加载更多的监听器,加载下一页
     * @author mrsimple
     */
    public interface OnLoadListener {
        void onPageLoad();
    }

}
